<?php

namespace app\live\controller;

use app\common\controller\Adminbase;
use app\model\Live as LiveModel;
use app\model\Shop;
use app\model\Talent;

class Live extends Adminbase
{
    public function index()
    {
        $model = 'Live';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list  = LiveModel::where($map)
                ->field('*')
                ->withAttr('live_status', function ($value) {
                    return $value == 0 ? '预约中' : '直播中';
                })
                ->withAttr('start_time', function ($value) {
                    return $value > 0 ? date('Y-m-d H:i:s', $value) : '';
                })
                ->withAttr('status', function ($value) {
                    $text = [1 => "<span class='layui-badge layui-bg-green'>显示</span>", 0 => "<span class='layui-badge'>隐藏</span>"];
                    return $text[$value];
                })
                ->page($page, $pageSize)
                ->order('status desc, sort asc, id desc')
                ->select();
            $total = LiveModel::where($map)->count();

            if ($list) {
                $talent_ids = $shop_ids = [];
                foreach ($list as $item) {
                    if ($item['type'] == 1) {
                        $talent_ids[] = $item['appid'];
                    } else {
                        $shop_ids[] = $item['appid'];
                    }
                }
                $index_arr = $talents = [];
                if ($talent_ids) {
                    $talents = Talent::where('talent_appid', 'in', $talent_ids)->column('talent_appid as appid, talent_nickname as name');
                    if ($talents) {
                        $index_arr = array_merge($index_arr, $talents);
                    }
                }
                if ($shop_ids) {
                    $shops = Shop::where('appid', 'in', $shop_ids)->column('appid, name');
                    if ($shops) {
                        $index_arr = array_merge($index_arr, $talents);
                    }
                }
                foreach ($list as &$item) {
                    $item['name'] = $index_arr[$item['appid']] ?? '';
                }
            }

            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDataUrl()
            ->setFormUrl('form')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'index_status')
            ->addTableColumn('id', 'ID', 100, null, true)
            ->addTableColumn('sort', '排序', 100, null, false, '', false, '', true)
            ->addTableColumn('appid', '主播ID', 180)
            ->addTableColumn('name', '主播昵称')
            ->addTableColumn('description', '直播间标题')
            ->addTableColumn('live_status', '直播状态')
            ->addTableColumn('start_time', '直播开始时间', 180)
            ->addTableColumn('number', '推广人数')
//            ->addTableColumn('talent_nickname', '来源')
//            ->addTableColumn('talent_nickname', '佣金')
            ->addTableColumn('index_status', '首页是否显示', null, '#switchTpl', false, '', false)
            ->addTableColumn('status', '状态')
            ->addTableOperation()
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'Live';

        $id = $this->request->param('id/d');
        if (isset($id) && $id > 0) {
            $info = LiveModel::get($id);
            $this->assign('id', $id);
            $this->assign('action', 2);
        } else {
            $info = [
                'sort' => 50,
                'type' => 2,
            ];
        }

        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setModel($model)
            ->setSuccessUrl()
            ->addFormItem('description', 'text', '直播间标题')
            ->addFormItem('sort', 'gtnumber', '排序', '值越小排序越靠前')
            ->addFormItem('number', 'gtnumber', '推广人数')
            ->addFormItem('index_status', 'radio', '首页是否显示', '', [1 => '显示', 0 => '隐藏'])
            ->addFormItem('status', 'radio', '状态', '', [1 => '显示', 0 => '隐藏'])
            ->setFormData($info)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }
}
